<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>FileCopy</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>FileCopy</h1>

<p>Copies one or more files.</p>

<pre class="Syntax">FileCopy, SourcePattern, DestPattern [, Flag]</pre>
<h3>Parameters</h3>
<dl>

  <dt>SourcePattern</dt>
  <dd><p>The name of a single file or folder, or a wildcard pattern such as C:\Temp\*.tmp. <em>SourcePattern</em> is assumed to be in <a href="../Variables.htm#WorkingDir">%A_WorkingDir%</a> if an absolute path isn't specified.</p></dd>

  <dt>DestPattern</dt>
  <dd>The name or pattern of the destination, which is assumed to be in <a href="../Variables.htm#WorkingDir">%A_WorkingDir%</a> if an absolute path isn't specified. To perform a simple copy -- retaining the existing file name(s) -- specify only the folder name as shown in these functionally identical examples:<br>
      <pre>FileCopy, C:\*.txt, C:\My Folder</pre>
      <pre>FileCopy, C:\*.txt, C:\My Folder\*.*</pre>
    </dd>

  <dt>Flag</dt>
  <dd><p>(optional) this flag determines whether to overwrite files
        if they already exist:</p>
      <p>0 = (default) do not overwrite existing files<br>
        1 = overwrite existing files</p>
      <p>This parameter can be an <a href="../Variables.htm#Expressions">expression</a>, even one that evalutes to true or false (since true and false are stored internally as 1 and 0).</p></dd>

</dl>

<h3>ErrorLevel</h3>
<p><span class="ver">[v1.1.04+]</span> This command is able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to the number of files that could not be copied due to an error, or 0 otherwise.</p>
<p>In either case, if the source file is a single file (no wildcards) and it does not exist, ErrorLevel is set to 0. To detect this condition, use <a href="IfExist.htm">IfExist</a> or <a href="../Functions.htm#FileExist">FileExist()</a> on the source file prior to copying it.</p>
<p>Unlike <a href="FileMove.htm">FileMove</a>, copying a file onto itself is always counted as an error, even if the overwrite mode is in effect.</p>
<p>If files were found, <a href="../Variables.htm#LastError">A_LastError</a> is set to 0 (zero) or the result of the operating system's GetLastError() function immediately after the last failure. Otherwise A_LastError contains an error code that might indicate why no files were found.</p>

<h3>Remarks</h3>
<p>FileCopy copies files only. To instead copy the contents of a folder (all its files and subfolders), see the examples section below. To copy a single folder (including its subfolders), use <a href="FileCopyDir.htm">FileCopyDir</a>.</p>
<p>The operation will continue even if error(s) are encountered.</p>
<h3>Related</h3>
<p><a href="FileMove.htm">FileMove</a>, <a href="FileCopyDir.htm">FileCopyDir</a>, <a href="FileMoveDir.htm">FileMoveDir</a>, <a href="FileDelete.htm">FileDelete</a></p>
<h3>Examples</h3>
<pre class="NoIndent">FileCopy, C:\My Documents\List1.txt, D:\Main Backup\  <em>; Make a copy but keep the orig. file name.</em>
FileCopy, C:\My File.txt, C:\My File New.txt  <em>; Copy a file into the same folder by providing a new name.</em>
FileCopy, C:\Folder1\*.txt, D:\New Folder\*.bkp  <em>; Copy to new location and give new extension.</em></pre>
<p>&nbsp;</p>
<pre class="NoIndent"><em>; The following example copies all files and folders inside a folder to a different folder:</em>
ErrorCount := CopyFilesAndFolders(&quot;C:\My Folder\*.*&quot;, &quot;D:\Folder to receive all files &amp; folders&quot;)
if ErrorCount &lt;&gt; 0
    MsgBox %ErrorCount% files/folders could not be copied.

CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
<em>; Copies all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be copied.</em>
{
    <em>; First copy all the files (but not the folders):</em>
    FileCopy, %SourcePattern%, %DestinationFolder%, %DoOverwrite%
    ErrorCount := ErrorLevel
    <em>; Now copy all the folders:</em>
    Loop, %SourcePattern%, 2  <em>; 2 means &quot;retrieve folders only&quot;.</em>
    {
        FileCopyDir, %A_LoopFileFullPath%, %DestinationFolder%\%A_LoopFileName%, %DoOverwrite%
        ErrorCount += ErrorLevel
        if ErrorLevel  <em>; Report each problem folder by name.</em>
            MsgBox Could not copy %A_LoopFileFullPath% into %DestinationFolder%.
    }
    return ErrorCount
}</pre>

</body>
</html>
